package com.bkgy.modular.subjectQuery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bkgy.modular.subjectQuery.entity.KeyValueEntity;
import com.bkgy.modular.subjectQuery.entity.ShareData;
import com.bkgy.modular.subjectQuery.entity.TbDsjC622FlatPassTime;
import com.bkgy.modular.subjectQuery.entity.TbDsjPdiC622;
import com.bkgy.modular.subjectQuery.mapper.TbDsjC622FlatPassTime2130Mapper;
import com.bkgy.modular.subjectQuery.mapper.TbDsjPdiC622Mapper;
import com.bkgy.modular.subjectQuery.service.RightDataTabService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName Cold
 * @date 2023/1/12 10:35
 * 2130冷轧PDI
 */
@Service("cold_pdi2130")
public class ColdRollingPdi2130Impl implements RightDataTabService {
    @Resource
    private TbDsjC622FlatPassTime2130Mapper tbDsjC622FlatPassTime2130Mapper;

    @Resource
    private TbDsjPdiC622Mapper tbDsjPdiC622Mapper;


    @Override
    public ResultData findByRightData(Integer pageIndex, Integer pageSize, String coilId, String hotId, ShareData shareData) {
        // 创建构造条件
        // 从Tb_DSJ_C622_FLAT_PASS_TIME表中使用coil_id查询出HOT_COIL_ID
        LambdaQueryWrapper<TbDsjC622FlatPassTime> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbDsjC622FlatPassTime::getCoil_id,coilId);

        // 调用mapper中的方法进行查询
        TbDsjC622FlatPassTime tbDsjC622FlatPassTime = tbDsjC622FlatPassTime2130Mapper.selectOne(queryWrapper);

        // 根据查询出的热卷号去TB_DSJ_PDI_C622查询表里所有的字段
        // 创建构造条件
        LambdaQueryWrapper<TbDsjPdiC622> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(TbDsjPdiC622::getCust_mat_no,tbDsjC622FlatPassTime.getHot_coil_id());

        List<TbDsjPdiC622> tbDsjPdiC622List = tbDsjPdiC622Mapper.selectList(queryWrapper1);
        List<KeyValueEntity> list = new ArrayList<>();
        for (TbDsjPdiC622 tbDsjPdiC622 : tbDsjPdiC622List) {
            Class c621Class = tbDsjPdiC622.getClass();
            Field[] declaredFields = c621Class.getDeclaredFields();
            for (int i = 0; i < declaredFields.length; i++) {
                Field field = declaredFields[i];
                field.setAccessible(true);
                KeyValueEntity keyValueEntity = new KeyValueEntity();
                keyValueEntity.setKey(field.getName());
                try {
                    keyValueEntity.setValue(field.get(tbDsjPdiC622));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                list.add(keyValueEntity);
            }
        }
        List<KeyValueEntity> entityList = list.stream().skip((pageIndex - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        return ResultData.ResultDataSuccess(entityList, list.size());
    }
}
